/*
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-05-18 15:43:05
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-05-18 20:43:03
 * @FilePath: \橙券\cq-serve\controller\news.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const News = require("../model/news");
const { publicUrl } = require("../config");
exports.getNews = async (req, res, next) => {
  try {
    let page = Number(req.query.page) ? parseInt(req.query.page) : 1;
    let size = Number(req.query.size) ? parseInt(req.query.size) : 9;
    console.log(page,size)
    let total = await News.find().count();
    let news = await News.find().skip((page - 1) * size).limit(size);
    news = news.map((item) => {
      return {
        _id: item._id,
        title: item.title,
        img: publicUrl + item.img.replace(/^\//, ""),
        createAt: item.createAt,
      };
    });
    res.json({
      code: 200,
      data: {
        total,
        news,
      },
    });
  } catch (error) {
    console.log(error);
  }
};

exports.getNewsDetail = async (req, res, next) => {
  try {
    const _id = req.params.id;
    const news = await News.findById(_id).select("title body createAt _id");
    res.json({
      code: 200,
      data: news,
    });
  } catch (error) {
    console.log(error);
  }
};
